package com.songcha.sakura.ui.adapter.diff

import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import com.songcha.sakura.download.base.DownloadTask
import com.songcha.sakura.download.m3u8.M3u8DownloadOption
import com.songcha.sakura.download.m3u8.M3u8DownloadTask



class DownloadTaskDiffCallback(var oldList:MutableList<DownloadTask>,var newList:MutableList<DownloadTask>) : DiffUtil.Callback() {
    companion object{
        const val PAYLOAD_FILE_NAME="fileName"
        const val PAYLOAD_PEOGRESS="progress"
        const val PAYLOAD_CURRENT_TS="current_ts"
        const val PAYLOAD_TOTAL_TS="total_ts"
        const val PAYLOAD_STATE="state"
        const val PAYLOAD_RESOLUTION="resolution"
    }

    override fun getOldListSize(): Int {
        return oldList.size
    }

    override fun getNewListSize(): Int {
        return newList.size
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].url==newList[newItemPosition].url
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldTask=oldList[oldItemPosition]
        val newTask=newList[newItemPosition]
        oldTask as M3u8DownloadTask
        newTask as M3u8DownloadTask
        if(oldTask.option.fileName!=newTask.option.fileName){
            return false
        }
        if(oldTask.progress!=newTask.progress){
            return false
        }
        if(oldTask.currentTsCount!=newTask.currentTsCount){
            return false
        }
        if(oldTask.totalTsCount!=newTask.totalTsCount){
            return false
        }
        if(oldTask.state!=newTask.state){
            return false
        }
        return true
    }

    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        val oldTask=oldList[oldItemPosition]
        val newTask=newList[newItemPosition]
        oldTask as M3u8DownloadTask
        newTask as M3u8DownloadTask
        oldTask.option as M3u8DownloadOption
        newTask.option as M3u8DownloadOption

        val payLoad=Bundle()
        if(oldTask.option.fileName!=newTask.option.fileName){
            payLoad.putString(PAYLOAD_FILE_NAME,newTask.option.fileName)
        }
        if(oldTask.option.resolution!=newTask.option.resolution){
            payLoad.putString(PAYLOAD_RESOLUTION,newTask.option.resolution)
        }
        if(oldTask.progress!=newTask.progress){
            payLoad.putInt(PAYLOAD_PEOGRESS,newTask.progress)
        }
        if(oldTask.currentTsCount!=newTask.currentTsCount){
            payLoad.putInt(PAYLOAD_CURRENT_TS,newTask.currentTsCount)
        }
        if(oldTask.totalTsCount!=newTask.totalTsCount){
            payLoad.putInt(PAYLOAD_TOTAL_TS,newTask.totalTsCount)
        }
        if(oldTask.state!=newTask.state){
            payLoad.putInt(PAYLOAD_STATE,newTask.state.ordinal)
        }
        return payLoad
    }

}